package com.wmobile.utils;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.wellsoft.mobileoa.R;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.SelectOpitonDataAdapter;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.wsmodel.WSOA_ActionDataSet;

public class PopOpinionDialog<WSOA_ActionData> extends Dialog {
	BaseActivity baseActivity;
	String title;
	List<Integer> selectedIndex;
	WSOA_ActionDataSet actionDataSet;
	String opinion;
	boolean isMultiSelect;
	Handler mHandler;
	int dialogResult;
	public static int DialogResultOK = 1;
	public static int DialogResultCANCEL = 0;

	TextView titleTv;
	ImageButton opinionIbn;
	ImageButton standPointIbn;
	Button confirmBtn;
	Button cancelBtn;
	EditText opinionEt;
	EditText standPointEt;
	LinearLayout standPointLl;

	public String getOpinion() {
		return opinion;
	}

	public List<Integer> getSelectedIndex() {
		return selectedIndex;
	}

	public int getDialogResult() {
		return dialogResult;
	}

	public PopOpinionDialog(BaseActivity baseActivity, String title,
			List<Integer> selectedIndex, WSOA_ActionDataSet actionDataSet,
			String opinion) {
		super(baseActivity, R.style.popDialogTheme);
		this.title = title;
		this.baseActivity = baseActivity;
		this.selectedIndex = selectedIndex;
		this.actionDataSet = actionDataSet;
		this.opinion = opinion;

		onCreate();
	}

	public void onCreate() {
		setContentView(R.layout.popopiniondialog);

		opinionEt = (EditText) findViewById(R.id.opinionet);
		opinionEt.setText(opinion);

		standPointEt = (EditText) findViewById(R.id.standpointet);
		if (selectedIndex.size() > 0) {
			standPointEt.setText(actionDataSet.getActionDatas()
					.get(selectedIndex.get(0)).getMessage());
		}

		titleTv = (TextView) findViewById(R.id.titletv);
		titleTv.setText(title);

		opinionIbn = (ImageButton) findViewById(R.id.opinionibn);
		opinionIbn.setOnClickListener(onOpinionBtnClickListener);
		opinionIbn.setOnTouchListener(onOpinionTouchListener);

		standPointLl = (LinearLayout) findViewById(R.id.standpointll);
		if (actionDataSet == null || actionDataSet.getActionDatas() == null
				|| actionDataSet.getActionDatas().size() == 0) {
			standPointLl.setVisibility(View.GONE);
		} else {
			standPointLl.setVisibility(View.VISIBLE);

			standPointIbn = (ImageButton) findViewById(R.id.standpointibn);
			standPointIbn.setOnClickListener(onStandPointBtnClickListener);
			standPointIbn.setOnTouchListener(onStandPointTouchListener);
		}

		confirmBtn = (Button) findViewById(R.id.confirmbtn);
		confirmBtn.setOnClickListener(onConfirmListener);
		confirmBtn.setVisibility(View.VISIBLE);

		cancelBtn = (Button) findViewById(R.id.cancelbtn);
		cancelBtn.setOnClickListener(onCancelListener);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			onCancelListener.onClick(null);
		}

		return super.onKeyDown(keyCode, event);
	}

	View.OnClickListener onOpinionBtnClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			List<Integer> selectedIndexes = new ArrayList<Integer>();
			SelectOpitonDataAdapter dataAdapter = new SelectOpitonDataAdapter(
					baseActivity, SystemConfig.getOpinions(), selectedIndexes,
					false);
			PopListDialog<String> dialog = new PopListDialog<String>(
					baseActivity, "常用语", selectedIndexes, dataAdapter, false);
			if (dialog.showDialog() == PopListDialog.DialogResultOK) {
				String content = opinionEt.getText().toString()
						+ SystemConfig.getOpinions().get(
								dialog.selectedIndex.get(0));
				opinionEt.setText(content);
				opinionEt.setSelection(content.length());
			}
		}

	};

	OnTouchListener onOpinionTouchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			ImageButton ib = (ImageButton) v;
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				ib.setBackgroundColor(Color.argb(127, 255, 255, 255));
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				ib.setBackgroundColor(Color.argb(0, 255, 255, 255));
			} else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
				ib.setBackgroundColor(Color.argb(0, 255, 255, 255));
			} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
				ib.setBackgroundColor(Color.argb(0, 255, 255, 255));
			}
			return false;
		}
	};

	View.OnClickListener onStandPointBtnClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			List<String> standPoints = new ArrayList<String>();
			for (int i = 0; i < actionDataSet.getActionDatas().size(); i++) {
				standPoints.add(actionDataSet.getActionDatas().get(i)
						.getMessage());
			}
			List<Integer> selectedIndexes = new ArrayList<Integer>();
			SelectOpitonDataAdapter dataAdapter = new SelectOpitonDataAdapter(
					baseActivity, standPoints, selectedIndexes, false);
			PopListDialog<String> dialog = new PopListDialog<String>(
					baseActivity, "选择意见立场", selectedIndexes, dataAdapter, false);
			if (dialog.showDialog() == PopListDialog.DialogResultOK) {
				selectedIndex = dialog.selectedIndex;
				standPointEt.setText(standPoints.get(selectedIndex.get(0)));
			}
		}

	};

	OnTouchListener onStandPointTouchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			ImageButton ib = (ImageButton) v;
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				ib.setBackgroundColor(Color.argb(127, 255, 255, 255));
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				ib.setBackgroundColor(Color.argb(0, 255, 255, 255));
			} else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
				ib.setBackgroundColor(Color.argb(0, 255, 255, 255));
			} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
				ib.setBackgroundColor(Color.argb(0, 255, 255, 255));
			}
			return false;
		}
	};

	View.OnClickListener onConfirmListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			endDialog(DialogResultOK);
		}
	};

	View.OnClickListener onCancelListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			endDialog(DialogResultCANCEL);
		}
	};

	public void endDialog(int result) {
		opinion = opinionEt.getText().toString();
		dismiss();
		dialogResult = result;
		Message m = mHandler.obtainMessage();
		mHandler.sendMessage(m);
	}

	public int showDialog() {
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message mesg) {
				throw new RuntimeException();
			}
		};
		super.show();
		try {
			Looper.getMainLooper();
			Looper.loop();
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		return dialogResult;
	}

}
